//
//  JJHomeOriginalView.swift
//  JJSina
//
//  Created by mac on 2017/12/6.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
import SnapKit

class JJHomeOriginalView: UIView {
    
    var orginalViewBottomConstraint: Constraint?
    
    // MARK: - 给子控件赋值
    var statusModel: JJStatusViewModel?{
        didSet{
            
            headImageView.jj_setImage(urlString: statusModel?.homeModel?.user?.profile_image_url)
            nameLabel.text = statusModel?.homeModel?.user?.name
            contentLabel.text = statusModel?.homeModel?.text
            vipImageView.image = statusModel?.VIPImage
            avatarImageView.image = statusModel?.avatarImage
            
            orginalViewBottomConstraint?.deactivate()
            
            // 判断是否有配图
            if let picUrls = statusModel?.homeModel?.pic_urls, picUrls.count > 0 {
                
                pictureView.pic_urls = picUrls
                
                self.snp.makeConstraints { (make) in
                
                    orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                pictureView.isHidden = false
            }else {
                // 没有配图
                // 约束
                self.snp.makeConstraints { (make) in
                    orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                
                // 隐藏
                pictureView.isHidden = true
                
            }
            
            timeLabel.text = "1212.2"
            sourceLabel.text = "中国"
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 添加视图
    private func setupUI() {
        backgroundColor = JJRandomColor()
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(avatarImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(10)
            make.width.height.equalTo(30)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(12)
            make.top.equalTo(headImageView.snp.top)
        }
        vipImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.left.equalTo(nameLabel.snp.right).offset(12)
        }
        avatarImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(12)
            make.bottom.equalTo(headImageView.snp.bottom)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeLabel.snp.centerY)
            make.left.equalTo(timeLabel.snp.right).offset(12)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            
        }
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
        make.top.equalTo(contentLabel.snp.bottom).offset(10)
        
        }
        snp.makeConstraints { (make) in
            orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    // MARK: - 懒加载子控件
    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default")
    private lazy var nameLabel: UILabel = UILabel(fontFloat: JJ_MIDDLE_FONTSIZE, textColor: UIColor.black, text: "心小帅帅")
    private lazy var vipImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    private lazy var timeLabel: UILabel = UILabel(fontFloat: JJ_SMALL_FONTSIZE, textColor: JJ_THEMECOLOR, text: "12-12")
    private lazy var sourceLabel: UILabel = UILabel(fontFloat: JJ_SMALL_FONTSIZE, textColor: JJ_THEMECOLOR, text: "weibo")
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vip")
    private lazy var contentLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.black
        lab.font = UIFont.systemFont(ofSize: JJ_MIDDLE_FONTSIZE)
        lab.numberOfLines = 0
        lab.backgroundColor = UIColor.white
        return lab
    }()
    private lazy var pictureView: JJHomePictureView = JJHomePictureView()
}













